package com.zzsy.carosshopprojects.activitys.home.mainpage.menu;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.MenuAdapter;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.base.DoHandoverCzBean;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.DoHandoverBean;
import com.zzsy.carosshopprojects.bean.MenuBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.bean.ResultBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ApkInfoUtil;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.SPUtil;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.MutableLiveData;

public class HomeMenuMode extends BaseViewMode {
    private MutableLiveData<List<MenuBean>> liveData;//获取抽屉界面显示数据
    private MutableLiveData<ResultBean> liveDataQrSwitch;

    /**
     * 设置menu显示的数据
     *
     * @param context
     */
    public void postHomeMenuListBean(Context context) {
        List<MenuBean> list = new ArrayList<>();

//        MenuBean menuBean = new MenuBean();
//        menuBean.setTitle(context.getResources().getString(R.string.menu_title));
//        menuBean.setSubtitle(context.getResources().getString(R.string.menu_subTitle));
//        menuBean.setViewType(MenuAdapter.TYPE_ITEM_TITLE);
//        list.add(menuBean);
        MenuBean menuBean = new MenuBean();
        menuBean.setTitle(SPUtil.getInstance().getString(ConfigureFile.COMPANYNAME, ""));
        menuBean.setSubtitle(SPUtil.getInstance().getString(ConfigureFile.STORENAME, ""));
        menuBean.setContent(SPUtil.getInstance().getString(ConfigureFile.NAME, ""));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_TITLE);
        list.add(menuBean);

//        menuBean = new MenuBean();
//        menuBean.setTitle(context.getResources().getString(R.string.menu_business));
//        menuBean.setContent(SPUtil.getInstance().getString(ConfigureFile.COMPANYNAME, ""));
//        menuBean.setViewType(MenuAdapter.TYPE_ITEM_BASE);
//        list.add(menuBean);
//
//        menuBean = new MenuBean();
//        menuBean.setTitle(context.getResources().getString(R.string.menu_store));
//        menuBean.setContent(SPUtil.getInstance().getString(ConfigureFile.STORENAME, ""));
//        menuBean.setViewType(MenuAdapter.TYPE_ITEM_BASE);
//        list.add(menuBean);

//        menuBean = new MenuBean();
//        menuBean.setTitle(context.getResources().getString(R.string.menu_account));
//        menuBean.setContent(SPUtil.getInstance().getString(ConfigureFile.NAME, ""));
//        menuBean.setViewType(MenuAdapter.TYPE_ITEM_BASE);
//        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_deviceNumber));
        menuBean.setContent(SPUtil.getInstance().getString(ConfigureFile.TERMINALNUM, ""));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_BASE);
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_edition));
        menuBean.setContent("V " + ApkInfoUtil.getVersionName(context));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_BASE);
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_INTERVAL);
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_history));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_HISTORY);
        list.add(menuBean);

        if (DataUtil.getInstance().getCustomerType().equals("1"))//1 加油员 有开具发票的功能 2 吧台 无此功能
        {
            menuBean = new MenuBean();
            menuBean.setTitle(context.getResources().getString(R.string.menu_switchInvoice));
            menuBean.setViewType(MenuAdapter.TYPE_ITEM_WHETHER);
            menuBean.setState(SPUtil.getInstance().getString(ConfigureFile.QRSWITCH, ""));
            menuBean.setSwitchType("1");
            list.add(menuBean);
        }

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_IsPrintOfficialAccountNote));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_WHETHER);
        menuBean.setState(SPUtil.getInstance().getString(ConfigureFile.GZHSWITCH, ""));
        menuBean.setSwitchType("3");
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_switchVoice));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_WHETHER);
        menuBean.setState(SPUtil.getInstance().getString(ConfigureFile.MUSICSWITCH, ""));
        menuBean.setSwitchType("2");
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_switchPrint));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_WHETHER);

        if (SPUtil.getInstance().getString(ConfigureFile.CHANGESHIFESTICKET, "").equals("")) {
            SPUtil.getInstance().putString(ConfigureFile.CHANGESHIFESTICKET, "1");
        }
        menuBean.setState(SPUtil.getInstance().getString(ConfigureFile.CHANGESHIFESTICKET, ""));
        menuBean.setSwitchType("4");
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setTitle(context.getResources().getString(R.string.menu_switchOtherPrint));
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_WHETHER);
        if (SPUtil.getInstance().getString(ConfigureFile.OTHERTICKET, "").equals("")) {
            SPUtil.getInstance().putString(ConfigureFile.OTHERTICKET, "1");
        }
        menuBean.setState(SPUtil.getInstance().getString(ConfigureFile.OTHERTICKET, ""));
        menuBean.setSwitchType("5");
        list.add(menuBean);

        menuBean = new MenuBean();
        menuBean.setViewType(MenuAdapter.TYPE_ITEM_INTERVAL);
        list.add(menuBean);

        liveData.postValue(list);
    }

    public MutableLiveData<List<MenuBean>> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<ResultBean> getLiveDataQrSwitch() {
        if (liveDataQrSwitch == null)
            liveDataQrSwitch = new MutableLiveData<>();
        return liveDataQrSwitch;
    }

    /**
     * 设置是否开具发票
     *
     * @param
     * @param qrSwitch
     */
    public void updateQrSwitch(Context context, String qrSwitch) {
        String userId = SPUtil.getInstance().getString(ConfigureFile.USERID, "");
        RequestUtils.UpdateQrSwitch(context, qrSwitch, userId, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    ResultBean resultBean = new ResultBean();
                    resultBean.setState(qrSwitch);
                    liveDataQrSwitch.postValue(resultBean);
                    SPUtil.getInstance().putString(ConfigureFile.QRSWITCH, qrSwitch);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                }


            }
        });
    }

    /**
     * 是谁交接
     *
     * @param context
     */
    public void DoHandoverWho(Context context) {
        String type = DataUtil.getInstance().getCustomerType();
        if (type.equals("1"))//操作员
            DoHandover(context);
        else if (type.equals("2"))//吧台
            DoRechargeHandover(context);
        else
            ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.accountError));

    }

    /**
     * 操作员交接班请求接口
     */
    private void DoHandover(Context context) {
        RequestUtils.DoHandover(context, DataUtil.getInstance().getTerminalId(), DataUtil.getInstance().getStoreId(), new MyObserver<List<DoHandoverCzBean>>(context) {
            @Override
            public void onSuccess(List<DoHandoverCzBean> result) {
                if (result != null) {
                    for (int i = 0; i < result.size(); i++) {
                        PrintHelper.getInstance().printDoHandover(result.get(i));
                    }

                }
                EventBus.getDefault().post(new MessageEvent(MessageEvent.JIAOBAN));
                //EventBus.getDefault().post(new MessageEvent(MessageEvent.CHECKORDERTICKET));
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    /**
     * 吧台交接班请求接口
     */
    private void DoRechargeHandover(Context context) {
        RequestUtils.DoRechargeHandover(context, DataUtil.getInstance().getTerminalId(), DataUtil.getInstance().getStoreId(), new MyObserver<DoHandoverBean>(context) {
            @Override
            public void onSuccess(DoHandoverBean result) {
                PrintHelper.getInstance().printDoRechargeHandover(result);
                EventBus.getDefault().post(new MessageEvent(MessageEvent.JIAOBAN));
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }
}






